﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using Microsoft.Practices.Unity;
using ScrumTable.Common.Text;
using ScrumTable.Config.ConfigManagement;
using ScrumTable.Config.ElementManagement;
using ScrumTable.Config.UserManagement;
using ScrumTable.UI.ViewModel.Data;

#endregion

namespace ScrumTable.UI.ViewModel.Controller
{
    /// <summary>
    /// Represents the method that handles the NavigationChanged event.
    /// </summary>
    /// <param name="sender">Specifies the sender of the event. Its the Controller.</param>
    /// <param name="e">Arguments which contain information about the event.</param>
    public delegate void ChangedEventHandler(object sender, NavigationEventArgs e);

    /// <summary>
    /// Represents the method that handles the LogOut event.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void LogOutEventHandler(object sender, EventArgs e);

    /// <summary>
    /// Represents the method that handles the LogIn event.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void LogInEventHandler(object sender, EventArgs e);


    /// <summary>
    /// Represents the method that handles the DataLogOutEventHandler.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void DataLogOutEventHandler(object sender, EventArgs e);


    /// <summary>
    /// Description of the class
    /// </summary>
    public class ScrumController
    {
        
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------        

   
        /// <summary>
        /// Occurs when [navigation changed].
        /// </summary>
        public event ChangedEventHandler ChangedView;

        /// <summary>
        /// Occurs when [someone has logged in].
        /// </summary>
        public event LogOutEventHandler LoggedOut;

        /// <summary>
        /// Occurs when [someone has logged out].
        /// </summary>
        public event LogInEventHandler LoggedIn;


        /// <summary>
        /// Occurs when [someone has removed his datacontext without loging out].
        /// </summary>
        public event DataLogOutEventHandler DataLogOut;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets or sets the container.
        /// </summary>
        /// <value>The container.</value>
        [Dependency]
        public IUnityContainer Container { get; set; }


        /// <summary>
        /// Gets the current NavigationTarget
        /// </summary>
        /// <value>The current NavigationTarget of the application.</value>
        public NavigationTarget CurrentTarget { get; private set; }

        
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------
     
        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        /// <summary>
        /// Moves the navigation to the saved view of the login account
        /// </summary>
        public void Login()
        {
            if (CurrentTarget == NavigationTarget.StartView || CurrentTarget == NavigationTarget.Null)
            {
                var manager = Container.Resolve<LoginAccountManager>();


                NavigationTarget target;

                if (string.IsNullOrEmpty(manager.CurrentLoginAccount.LastUsedScreen)
                    || !manager.CurrentLoginAccount.LastUsedScreen.TryParseEnum(out target))
                {
                    target = NavigationTarget.ScrumMap; 
                }
                if( target == NavigationTarget.StartView)
                {
                    target = NavigationTarget.ScrumMap;
                }
                Container.Resolve<ScrumViewModelData>().SetCurrentProjectAndIteration(manager.CurrentLoginAccount.ProjectId, manager.CurrentLoginAccount.IterationId);
                Container.Resolve<ConfigManager>().SetCurrentConfig(manager.CurrentLoginAccount.ConfigId);
                GoTo( target );
            }
            OnLogIn();
        }

        /// <summary>
        /// GoTo StartView
        /// </summary>
        public void LogOut()
        {
            OnLogOut();
            Container.Resolve<LoginAccountManager>().SaveLogin();
            Container.Resolve<ConfigManager>().Save();
            Container.Resolve<ElementManager>().Save();
            GoTo(NavigationTarget.StartView);
        }

        /// <summary>
        /// Fires the DataLogOutEvent
        /// </summary>
        public void FireDataLogOut()
        {
            OnDataLogOut();
        }

        /// <summary>
        /// Change the NavigationTarget
        /// </summary>
        /// <param name="to">Go To</param>
        /// <returns></returns>
        public void GoTo(NavigationTarget to)
        {
            if (NavigationTarget.Null != to && CurrentTarget != to)
            {
                NavigationEventArgs eventArgs = new NavigationEventArgs(to);
                CurrentTarget = to;
                OnChanged(eventArgs);
                
            }
        }   

        
        #endregion

        #region Events       
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------
        /// <summary>
        /// Raises the <see cref="ChangedView"/> event.
        /// </summary>
        /// <param name="eventArgs">The <see cref="ScrumTable.UI.ViewModel.Controller.NavigationEventArgs"/> instance containing the event data.</param>
        protected virtual void OnChanged(NavigationEventArgs eventArgs)
        {
            if (ChangedView != null)
            {
                ChangedView(this, eventArgs);
            }
        }

        /// <summary>
        /// Raises the <see cref="LogOut"/> event.
        /// </summary>
        protected virtual void OnLogOut()
        {
            if (LoggedOut != null)
            {
                LoggedOut(this, new EventArgs());
            }
        }

        /// <summary>
        /// </summary>
        protected virtual void OnLogIn()
        {
            if (LoggedIn != null)
            {
                LoggedIn(this, new EventArgs());
            }
        }

        /// <summary>
        /// Raises the <see cref="DataLogOut"/> event.
        /// </summary>
        protected virtual void OnDataLogOut()
        {
            if (DataLogOut != null)
            {
                DataLogOut(this, new EventArgs());
            }
        }
        #endregion

    }
}